home *** CD-ROM | disk | FTP | other *** search
/ An Introduction to Progr…l Basic 6.0 (4th Edition) / An Introduction to Programming using Visual Basic 6.0.iso / PROGRAMS / CH10 / 10-4-5.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1998-10-19  |  4.5 KB  |  122 lines

  1. VERSION 5.00
  2. Begin VB.Form frm10_4_5 
  3.    Caption         =   "Browser Market Share"
  4.    ClientHeight    =   5748
  5.    ClientLeft      =   1908
  6.    ClientTop       =   1740
  7.    ClientWidth     =   8292
  8.    BeginProperty Font 
  9.       Name            =   "MS Sans Serif"
  10.       Size            =   7.8
  11.       Charset         =   0
  12.       Weight          =   700
  13.       Underline       =   0   'False
  14.       Italic          =   0   'False
  15.       Strikethrough   =   0   'False
  16.    EndProperty
  17.    LinkTopic       =   "Form1"
  18.    PaletteMode     =   1  'UseZOrder
  19.    ScaleHeight     =   5748
  20.    ScaleWidth      =   8292
  21.    Begin VB.PictureBox picShare 
  22.       Height          =   4935
  23.       Left            =   600
  24.       ScaleHeight     =   4884
  25.       ScaleWidth      =   7044
  26.       TabIndex        =   1
  27.       Top             =   720
  28.       Width           =   7095
  29.    End
  30.    Begin VB.CommandButton cmdDraw 
  31.       Caption         =   "Draw Market Share Pie Chart"
  32.       Height          =   495
  33.       Left            =   2640
  34.       TabIndex        =   0
  35.       Top             =   120
  36.       Width           =   2775
  37.    End
  38. Attribute VB_Name = "frm10_4_5"
  39. Attribute VB_GlobalNameSpace = False
  40. Attribute VB_Creatable = False
  41. Attribute VB_PredeclaredId = True
  42. Attribute VB_Exposed = False
  43. Private Sub cmdDraw_Click()
  44.   Dim numItems As Integer, radius As Single
  45.   'Draw pie chart of Browser Market Share
  46.   numItems = 3
  47.   ReDim category(1 To numItems) As String
  48.   ReDim quantity(1 To numItems) As Single
  49.   Call ReadData(category(), quantity(), numItems)
  50.   Call DrawData(quantity(), numItems, radius)
  51.   Call ShowLegend(category(), numItems, radius)
  52.   Call ShowTitle(radius)
  53. End Sub
  54. Private Sub DrawData(quantity() As Single, numItems As Integer, radius As Single)
  55.   Dim circumf As Single, leftEdge As Single, rightEdge As Single
  56.   Dim topEdge As Single, bottomEdge As Single, i As Integer
  57.   Dim startAngle As Single, stopAngle As Single
  58.   'Draw and fill each sector of pie chart
  59.   'All scaling and text positioning done as a percentage of radius
  60.   radius = 1  'actual value used is not important
  61.   'Make picture 4 radii wide to provide plenty of space for
  62.   'circle and legends. Place origin 1.25 radii from left edge;
  63.   'space of 1.75 radii will remain on right for legends.
  64.   leftEdge = -1.25 * radius
  65.   rightEdge = 2.75 * radius
  66.   'Force vertical scale to match horizontal scale;
  67.   'center origin vertically
  68.   topEdge = 2 * radius * (picShare.Height / picShare.Width)
  69.   bottomEdge = -topEdge
  70.   picShare.Cls
  71.   picShare.Scale (leftEdge, topEdge)-(rightEdge, bottomEdge)
  72.   circumf = 2 * 3.14159
  73.   ReDim cumPercent(0 To numItems) As Single
  74.   cumPercent(0) = 0.0000001 'a value of "zero" that can be made negative
  75.   For i = 1 To numItems
  76.     cumPercent(i) = cumPercent(i - 1) + quantity(i)
  77.     startAngle = cumPercent(i - 1) * circumf
  78.     stopAngle = cumPercent(i) * circumf
  79.     picShare.FillStyle = (8 - i) 'use fill patterns 7, 6, and 5
  80.     picShare.Circle (0, 0), radius, , -startAngle, -stopAngle
  81.   Next i
  82. End Sub
  83. Private Sub Locate(x As Single, y As Single)
  84.   picShare.CurrentX = x
  85.   picShare.CurrentY = y
  86. End Sub
  87. Private Sub ReadData(category() As String, quantity() As Single, numItems As Integer)
  88.   Dim i As Integer
  89.   'Load categories and percentages of market share
  90.   'Assume the data has been placed in the file BROWSERS.DAT
  91.   '(First line in file is "Internet Explorer", .44)
  92.   Open App.Path & "\BROWSERS.TXT" For Input As #1
  93.   For i = 1 To numItems
  94.     Input #1, category(i), quantity(i)
  95.   Next i
  96.   Close #1
  97. End Sub
  98. Private Sub ShowLegend(category() As String, numItems As Integer, radius As Single)
  99.   Dim lblHght As Single, legendSize As Single
  100.   Dim i As Integer, vertPos As Single
  101.   'Place legend centered to right of pie chart
  102.   'Make separation between items equal to one line of text
  103.   '"Text lines" needed for legends is thus (2*numItems-1)
  104.   lblHght = picShare.TextHeight(" ")
  105.   legendSize = lblHght * (2 * numItems - 1)
  106.   For i = 1 To numItems
  107.     picShare.FillStyle = 8 - i
  108.     vertPos = (legendSize / 2) - (3 - i) * (2 * lblHght)
  109.     picShare.Line (1.1 * radius, vertPos)-(1.4 * radius, vertPos + lblHght), , B
  110.     Call Locate(1.5 * radius, vertPos)
  111.     picShare.Print category(i)
  112.   Next i
  113. End Sub
  114. Private Sub ShowTitle(radius As Single)
  115.   Dim lbl As String, lblWid As Single
  116.   'Display title right below circle
  117.   lbl = "Browser Market Share, July 1998"
  118.   lblWid = picShare.TextWidth(lbl)
  119.   Call Locate(-lblWid / 2, -(radius + 0.05))
  120.   picShare.Print lbl
  121. End Sub
  122.